// Copyright 2007-2009 Stuart Caborn, Alex Scordellis
//  
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License. You may obtain a copy of
//  the License at
//  
//  http://www.apache.org/licenses/LICENSE-2.0
//  
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
//  License for the specific language governing permissions and limitations under
//  the License.
// 

using System;
using System.Collections.Generic;
using System.Reflection;

namespace NSynthesis
{
    [Serializable]
    public class Recorder : IRecorder
    {
        private readonly Dictionary<MethodInfo, MockedMethodCall> mockedMethodCalls = new Dictionary<MethodInfo, MockedMethodCall>();
        private readonly Dictionary<MethodInfo, RealMethodCall> realMethodCalls = new Dictionary<MethodInfo, RealMethodCall>();


        public void SaveReport()
        {
            Console.Write("Generating NSynthesis Report...");
            var report = new Report(MissingMethods, mockedMethodCalls.Values, realMethodCalls.Values);
            report.Save();
            Console.WriteLine("Done.");
        }

        private IEnumerable<MockedMethodCall> MissingMethods
        {
            get
            {
                foreach (var mockedCall in mockedMethodCalls.Values)
                {
                    if (mockedCall.IsNotResolved) yield return mockedCall;
                }
            }
        }

        public IEnumerable<RealMethodCall> RealMethodCalls
        {
            get { return realMethodCalls.Values; }
        }

        public IEnumerable<MockedMethodCall> MockedMethodCalls
        {
            get { return mockedMethodCalls.Values; }
        }

        IList<MockedMethodCall> IRecorder.MissingMethods
        {
            get { return new List<MockedMethodCall>(MissingMethods); }
        }

        public void RegisterRealMethodCall(MethodInfo registeredCall)
        {
            if (!realMethodCalls.ContainsKey(registeredCall))
                realMethodCalls.Add(registeredCall, new RealMethodCall(registeredCall));

            realMethodCalls[registeredCall].Hits++;
        }

        public void RegisterMockMethodCall(MethodInfo registeredCall)
        {
            if (!mockedMethodCalls.ContainsKey(registeredCall))
                mockedMethodCalls.Add(registeredCall, new MockedMethodCall(registeredCall));
            
            mockedMethodCalls[registeredCall].Hits++;
        }

    }
}